package com.ls.lserp.modules.trade.service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.modules.order.dao.AuditInfoDao;
import com.ls.lserp.modules.order.dao.OrderDao;
import com.ls.lserp.modules.order.entity.AuditInfo;
import com.ls.lserp.modules.order.entity.Order;
import com.ls.lserp.modules.setting.entity.Ognization;
import com.ls.lserp.modules.sys.utils.DictUtils;
import com.ls.lserp.modules.sys.utils.UserUtils;
import com.ls.lserp.modules.trade.dao.CustomEnchaseDao;
import com.ls.lserp.modules.trade.entity.CartonInfo;
import com.ls.lserp.modules.trade.entity.CustomEnchase;
import com.ls.lserp.modules.trade.entity.Enchase;

/**
 * @ClassName: EnchaseService
 * @Description: 装箱单
 * 
 * @author
 * @date 2017年3月24日 下午18:40:40
 *
 */

@Service
@Transactional(readOnly = true)
public class CustomEnchaseService extends CrudService<CustomEnchaseDao, CustomEnchase> {

	@Autowired
	private CustomEnchaseDao enchaseDao;
	@Autowired
	private OrderDao orderDao;
	// 审核表
    @Autowired
    private AuditInfoDao  auditInfoDao;

	public static final String PACKAGE_NO = "E";

	/**
	 * @Title: findList @Description: 查询 @param @param page @param @param
	 * enchase @param @return @return Page<HandBook> @throws
	 */
	public Page<CustomEnchase> findList(Page<CustomEnchase> page, CustomEnchase enchase) {

		// tab页分，检索条件设定(0:全部,1:待装箱,2:已装箱,3:已删除)
		int tabFlg = enchase.getTabPageStatus();
		enchase.setEnchaseDelFlg(Enchase.DEL_FLAG_NORMAL);
		if (1 == tabFlg) {
			enchase.setPackageStatus("");
			enchase.setPackageAuditStatus(ResourcesUtil.getConfig("audit_uncommit"));
		} else if (2 == tabFlg) {
			enchase.setPackageStatus("");
			enchase.setPackageAuditStatus(ResourcesUtil.getConfig("reserver_meterial_wait_accept"));
		} else if (3 == tabFlg) {
			enchase.setPackageStatus("");
			enchase.setPackageAuditStatus(ResourcesUtil.getConfig("reserver_meterial_ok"));
		} else if (4 == tabFlg) {
			enchase.setPackageStatus("");
			enchase.setPackageAuditStatus(ResourcesUtil.getConfig("reserver_meterial_no"));
		} else if (5 == tabFlg) {
			enchase.setPackageStatus("1");
			enchase.setPackageAuditStatus(ResourcesUtil.getConfig("reserver_meterial_ok"));
		} else if (6 == tabFlg) {
			enchase.setPackageStatus("2");
			enchase.setPackageAuditStatus(ResourcesUtil.getConfig("reserver_meterial_ok"));
		} else if (9 == tabFlg) {
			enchase.setEnchaseDelFlg(Enchase.DEL_FLAG_DELETE);
			enchase.setPackageStatus("");
			enchase.setPackageAuditStatus("");
		} else {
			enchase.setPackageStatus("");
			enchase.setPackageAuditStatus("");
		}

		// 设置分页参数
		enchase.setPage(page);

		List<CustomEnchase> resultList = enchaseDao.findList(enchase);

		// 执行分页查询
		page.setList(resultList);

		return page;
	}

	/**
	 * @Title: findCartonInfoList @Description: 查询纸箱 @param @param
	 * cartonInfo @param @return @return List<CartonInfo> @throws
	 */

	public List<CartonInfo> findCartonInfoList(CartonInfo cartonInfo) {

		return enchaseDao.findCartonInfoList(cartonInfo);
	}

	/**
	 * @Title: findIndentList @Description: 查询订单（颜色，尺寸分） @param @param
	 * enchase @param @return @return List<Enchase> @throws
	 */

	public List<CustomEnchase> findIndentList(Long indentId) {
		CustomEnchase pEnchase = new CustomEnchase();
		pEnchase.setIndentId(indentId);
		return enchaseDao.findIndentList(pEnchase);
	}

	/**
	 * @Title: findSizeList @Description: 获得所有尺码一览 @param @param
	 * enchase @param @return @return List<Enchase> @throws
	 */

	public List<CustomEnchase> findSizeList(Long indentId) {
		CustomEnchase pEnchase = new CustomEnchase();
		pEnchase.setIndentId(indentId);
		return enchaseDao.findSizeList(pEnchase);
	}

	/**
	 * @Title: saveDict @Description: 保存基准数据 @param @param dict @return
	 * void @throws
	 */
	@Transactional(readOnly = false)
	public void saveEnchase(CustomEnchase enchase) {

		// 判断新建编辑
		// 若为null 执行插入语句（新建）
		if (enchase.getId() == null) {
			enchase.preInsert();
			enchase.setVersion(1L);

			// 取得手册编号
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String dateNowStr = sdf.format(new Date());
			enchase.setPackageNo(PACKAGE_NO + "." +enchase.getOgnizationPEntity().getOgnizationSimpleName()+ "." + dateNowStr);
			enchase.setPackageStatus("1");
			enchase.setPackageAuditStatus(ResourcesUtil.getConfig("audit_uncommit"));
			enchaseDao.insertPackage(enchase);

			// 编辑
		} else {
			// 更新数据
			enchase.preUpdate();
			enchaseDao.update(enchase);
			// 删除出口成本数据
			enchaseDao.deletePackageDetails(enchase);
		}

		// 判断list是否为空
		if (!CollectionsUtils.isEmpty(enchase.getEnchaseList())) {
			enchaseDao.insertPackageDetail(enchase);
			for (int i = 0; i < enchase.getEnchaseList().size(); i++) {
				enchase.getEnchaseList().get(i).preUpdate();
				enchaseDao.updateBarCode(enchase.getEnchaseList().get(i));
			}
		}
	}

	/**
	 * check BigDecimal
	 * 
	 * @param cost
	 * @return 取不到返回0
	 */
	public BigDecimal checkBigDecimalNull(BigDecimal cost) {
		if (cost == null) {
			return new BigDecimal("0");
		}
		return cost;
	}

	/**
	 * @Title: findPackageDetailsList @Description: 箱单一览 @param @param
	 * enchase @param @return @return List<Enchase> @throws
	 */

	public List<CustomEnchase> findPackageDetailsList(CustomEnchase enchase) {
		return enchaseDao.findPackageDetailsList(enchase);
	}

	/**
	 * @Title: setPackageDetailsList @Description: 箱单明细一览 @param @param
	 * enchase @param @return @return List<Enchase> @throws
	 */

	public List<CustomEnchase> setPackageDetailsList(List<CustomEnchase> packageDetailsList,
			List<CustomEnchase> allSizeList) {
		// 明细sort
		long preSort = -1;
		// 返回箱单明细list
		List<CustomEnchase> returnEnchaseList = new ArrayList<CustomEnchase>();
		// 新明细信息
		CustomEnchase tempEnchse = null;

		// sort相同的数据合并成一条数据
		for (CustomEnchase forEnchase : packageDetailsList) {
			if (preSort != forEnchase.getPackageDetailsSort()) {
				tempEnchse = new CustomEnchase();
				tempEnchse.setAllSizeList(getNewSizeList(allSizeList));
				// 国家cd
				tempEnchse.setCountryCd(forEnchase.getCountryCd());
				// 纸箱id
				tempEnchse.setCartonId(forEnchase.getCartonId());
				// 纸箱名
				tempEnchse.setCartonName(forEnchase.getCartonName());
				// 箱号from
				tempEnchse.setCartonNoFrom(forEnchase.getCartonNoFrom());
				// 箱号to
				tempEnchse.setCartonNoTo(forEnchase.getCartonNoTo());
				// 装箱类别
				tempEnchse.setPackageStyle(forEnchase.getPackageStyle());
				// 箱数
				tempEnchse.setCartonQuantity(forEnchase.getCartonQuantity());
				// 颜色
				tempEnchse.setCartonColorName(forEnchase.getCartonColorName());
				// 净重
				tempEnchse.setCartonNetWeight(forEnchase.getCartonNetWeight());
				// 客户合同号
				tempEnchse.setCustomerContractNo(forEnchase.getCustomerContractNo());
				// 客户款号
				tempEnchse.setStyleCustomerNo(forEnchase.getStyleCustomerNo());
				// 纸箱体积
				tempEnchse.setBoxVolume(forEnchase.getBoxVolume());
				// 纸箱重量
				tempEnchse.setBoxWeight(forEnchase.getBoxWeight());
				// 每箱件
				tempEnchse.setSingleCarton(forEnchase.getSingleCarton());
				returnEnchaseList.add(tempEnchse);
				preSort = forEnchase.getPackageDetailsSort();
			}
			// 设定尺码所对应的衣服件数
			if (StringUtils.isNotEmpty(forEnchase.getCartonSizeCd())) {
				for (CustomEnchase forSize : tempEnchse.getAllSizeList()) {
					if (forSize.getCartonSizeCd().equals(forEnchase.getCartonSizeCd())) {
						forSize.setCartonStyleQuantity(forEnchase.getCartonStyleQuantity());
					}
				}
			}
		}

		List<String> repeatValue = new ArrayList<String>();
		String strFromTo = "";
		long countMoreCarton = 0L;
		// 设定总件数和设定箱号重复标记
		for (CustomEnchase ec : returnEnchaseList) {
			strFromTo = ec.getCartonNoFrom() + "," + ec.getCartonNoTo();
			// 判断箱号是否相同
			if (repeatValue.contains(strFromTo)) {
				ec.setHasCartonFromTo("1");
			} else {
				ec.setHasCartonFromTo("0");
				repeatValue.add(strFromTo);
			}

			// 总件数
			countMoreCarton = ec.getCartonQuantity() * ec.getSingleCarton();
			ec.setMoreCarton(countMoreCarton);
		}

		return returnEnchaseList;
	}

	/**
	 * @Title: getNewSizeList @Description: 新建尺码list @param @param
	 * enchase @param @return @return List<Enchase> @throws
	 */
	public List<CustomEnchase> getNewSizeList(List<CustomEnchase> allSizeList) {
		List<CustomEnchase> pEnchaseList = new ArrayList<CustomEnchase>();
		CustomEnchase sizeInfo = null;
		int allSizeListSize = 0;
		if (!CollectionsUtils.isEmpty(allSizeList)) {
			allSizeListSize = allSizeList.size();
		}

		// 箱单明细的每箱尺寸搭配只显示8个
		for (int i = 0; i < 8; i++) {
			sizeInfo = new CustomEnchase();
			sizeInfo.setSizeName("");
			sizeInfo.setCartonSizeCd("");
			if (i < allSizeListSize) {
				sizeInfo.setSizeName(allSizeList.get(i).getSizeName());
				sizeInfo.setCartonSizeCd(allSizeList.get(i).getCartonSizeCd());
			}
			pEnchaseList.add(sizeInfo);
		}
		return pEnchaseList;
	}

	/**
	 * @Title: findShowIndentList @Description: 设定款式信息的待装箱数 @param @param
	 * enchase @param @return @return List<Enchase> @throws
	 */

	public List<CustomEnchase> findShowIndentList(List<CustomEnchase> packageDetailsList,
			List<CustomEnchase> indentList) {
		long waitNum = 0L;
		long styleQuantity = 0L;
		for (CustomEnchase e : indentList) {
			for (CustomEnchase pdl : packageDetailsList) {
				if (e.getCartonColorName().equals(pdl.getCartonColorName())) {
					for (CustomEnchase asl : pdl.getAllSizeList()) {
						if (asl.getCartonSizeCd().equals(e.getCartonSizeCd())) {
							if (asl.getCartonStyleQuantity() == null) {
								styleQuantity = 0L;
							} else {
								styleQuantity = asl.getCartonStyleQuantity().longValue();
							}
							waitNum = waitNum + (styleQuantity * pdl.getCartonQuantity());
						}
					}
				}
			}
			e.setUnPackNumber(e.getIndentNumber() - waitNum);
			waitNum = 0L;
		}
		return indentList;
	}

	/**
	 * @Title: findShowCartonInfoList @Description: 设定纸箱信息 @param @param
	 * enchase @param @return @return List<Enchase> @throws
	 */

	public List<CartonInfo> findShowCartonInfoList(List<CustomEnchase> packageDetailsList) {
		String commaCartonId = "";
		List<String> cartonIdList = new ArrayList<String>();
		long count = 0L;

		// 获取纸箱id
		for (CustomEnchase e : packageDetailsList) {
			if (e.getCartonId() != null) {
				if (!cartonIdList.contains(String.valueOf(e.getCartonId().longValue()))) {
					cartonIdList.add(String.valueOf(e.getCartonId().longValue()));
				}
			}
		}

		// 查询纸箱信息
		List<CartonInfo> returnCartonInfoList = new ArrayList<CartonInfo>();
		if (!CollectionsUtils.isEmpty(cartonIdList)) {
			commaCartonId = CollectionsUtils.convertToString(cartonIdList, ",");

			CartonInfo ci = new CartonInfo();
			ci.setCommaCartonId(commaCartonId);

			returnCartonInfoList = enchaseDao.findCartonInfoList(ci);
		}

		// 计算总箱数
		for (CartonInfo forCi : returnCartonInfoList) {
			for (CustomEnchase pdl : packageDetailsList) {
				if (pdl.getHasCartonFromTo().equals("0") && forCi.getId().equals(pdl.getCartonId())) {
					count += pdl.getCartonQuantity();
				}
			}
			forCi.setCartonNumber(count);
			count = 0L;
		}
		return returnCartonInfoList;
	}

	/**
	 * @Title: deleteEnchase @Description: 删除箱单(逻辑删除) @param @param
	 * enchase @return void @throws
	 */

	@Transactional(readOnly = false)
	public void deleteEnchase(CustomEnchase enchase) {
		// 执行逻辑删除
		enchaseDao.delete(enchase);
	}

	/**
	 * @Title: findColorList @Description: 设定颜色下拉 @param @param
	 * enchase @param @return @return List<String> @throws
	 */

	public List<String> findColorList(List<CustomEnchase> indentList) {
		List<String> colorList = new ArrayList<String>();
		for (CustomEnchase e : indentList) {
			if (StringUtils.isNotEmpty(e.getCartonColorName())) {
				if (!colorList.contains(e.getCartonColorName())) {
					colorList.add(e.getCartonColorName());
				}
			}
		}
		return colorList;
	}

	/**
	  * @Title: saveAudit
	  * @Description: 保存履历信息
	  * @param @param auditInfo
	  * @return void 
	  * @throws
	  */
	@Transactional(readOnly = false)
	public void commitEnchase(CustomEnchase enchase) {
		
		enchase.preUpdate();
		enchase.setPackageAuditStatus(ResourcesUtil.getConfig("reserver_meterial_wait_accept"));
	    enchaseDao.commitEnchase(enchase);
	}
	
	/**
     * 获取客户列表
     * @return
     */
    public List<Ognization> findCustomList(){
    	return orderDao.findCustomList();
    }
	
	/**
	 * @Title: findOrderInfoByDate @Description: 制作箱单list @param @param
	 * enchase @param @return @return List<CustomEnchase> @throws
	 */

	public CustomEnchase findOrderInfoByDate(CustomEnchase enchase,int flagNum) throws Exception{
		List<CustomEnchase> contractList = enchaseDao.getContractInfo(enchase);
		List<Order> orList = new ArrayList<>();
		List<Order> orListSS = new ArrayList<>();
		//新建
		if (flagNum == 0) {
			orListSS = enchaseDao.findOrderInfoByDateSS(enchase);
			orList = enchaseDao.findOrderInfoByDate(enchase);
		//查看
		}else{
			orListSS = enchaseDao.findPackageDetailByIdSS(enchase);
			orList = enchaseDao.findPackageDetailById(enchase);
		}
		CustomEnchase smallEnchase = new CustomEnchase();
		CustomEnchase bigEnchase = new CustomEnchase();
		List<Order> smallList = new ArrayList<>();
		List<CustomEnchase> bigList = new ArrayList<>();
		List<CustomEnchase> allList = new ArrayList<>();
		Format f1 = new DecimalFormat("00");
		Format f2 = new DecimalFormat("000");
		String dateStr = enchase.getTargetDateStr().split("-")[1] + enchase.getTargetDateStr().split("-")[2] + ".";
		//大箱跨越行数
		int rowSpan = 0;
		//大箱连翻号
		int bigNo = 1;
		//计数大箱数量
		int bigCount = 0;
		//小箱连翻号
		int smallNo = 1;
		//计数小箱数量
		int smallCount = 0;
		//计数小箱数量(无大箱)
		int sSmallCount = 0;
		int count = 0;
		int smallCartonNum = 0;
		Long bigCartonId = enchase.getBigCartonId();
		Long smallCartonId = enchase.getSmallCartonId();
		Long bigCartonType = enchase.getBigCartonType();
		Long smallCartonType = enchase.getSmallCartonType();
		Double bigCartonWeight = enchase.getBigCartonWeight();
		Double smallCartonWeight = enchase.getSmallCartonWeight();
		Double bigCartonVolume = enchase.getBigCartonVolume();
		Double smallCartonVolume = enchase.getSmallCartonVolume();
		//SS澳大利亚
		if (orListSS != null && orListSS.size() > 0) {
			for (int i = 0; i < orListSS.size(); i++) {
				if (orListSS.get(i).getShopName() == null || "".equals(orListSS.get(i).getShopName())) {
					throw new RuntimeException(orListSS.get(i).getOrderNO()+"订单装箱信息不全！Info_Incomplete"); 
				}
				if (i==0&&i<orListSS.size()-1&&orListSS.get(i).getShippingDestintion().equals(orListSS.get(i+1).getShippingDestintion())) {
					if (orListSS.get(i).getShopName().equals(orListSS.get(i+1).getShopName())) {
						smallList.add(orListSS.get(i));
						count++;
						rowSpan++;
					}else {
						smallList.add(orListSS.get(i));
						smallEnchase.setSmallCartonId(smallCartonId);
						if (flagNum == 0) {
							smallEnchase.setSmallCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo)+"."+f2.format(smallNo));
						}else{
							smallEnchase.setSmallCartonNo(orListSS.get(i).getSmallCartonNo());
						}
						smallNo = 0;
						smallCount++;
						sSmallCount++;
						smallEnchase.setEnchaseList(smallList);
						bigList.add(smallEnchase);
						if (flagNum == 0) {
							bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "."+f1.format(bigNo));
						}else{
							bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
						}
						bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
						bigEnchase.setEnchases(bigList);
						bigEnchase.setRowSpan(1);
						allList.add(bigEnchase);
						smallEnchase = new CustomEnchase();
						smallList = new ArrayList<>();
						bigEnchase = new CustomEnchase();
						bigList = new ArrayList<>();
					}
				}else if ((i==0&&orListSS.size()==1)||(i==0&&i<orListSS.size()-1&&!orListSS.get(i).getShippingDestintion().equals(orListSS.get(i+1).getShippingDestintion()))) {
					smallList.add(orListSS.get(i));
					smallEnchase.setSmallCartonId(smallCartonId);
					if (flagNum == 0) {
						smallEnchase.setSmallCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase() +"."+f1.format(bigNo)+"."+f2.format(smallNo));
					}else{
						smallEnchase.setSmallCartonNo(orListSS.get(i).getSmallCartonNo());
					}
					smallNo = 1;
					smallCount++;
					sSmallCount++;
					smallEnchase.setEnchaseList(smallList);
					bigList.add(smallEnchase);
					bigEnchase.setBigCartonId(bigCartonId);
					if (flagNum == 0) {
						bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "."+f1.format(bigNo));
					}else{
						bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
					}
					bigNo = 1;
					bigCount++;
					bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
					bigEnchase.setEnchases(bigList);
					bigEnchase.setRowSpan(1);
					allList.add(bigEnchase);
					smallEnchase = new CustomEnchase();
					smallList = new ArrayList<>();
					bigEnchase = new CustomEnchase();
					bigList = new ArrayList<>();
				}else if (i>0&&i<orListSS.size()-1&&orListSS.get(i).getShippingDestintion().equals(orListSS.get(i+1).getShippingDestintion())){
					smallList.add(orListSS.get(i));
					count++;
					rowSpan++;
					if (orListSS.get(i).getShopName().equals(orListSS.get(i+1).getShopName())) {
						if (count==smallCartonType) {
							smallCartonNum++;
							count = 0;
							smallEnchase.setSmallCartonId(smallCartonId);
							if (flagNum == 0) {
								smallEnchase.setSmallCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo)+"."+f2.format(smallNo));
							}else{
								smallEnchase.setSmallCartonNo(orListSS.get(i).getSmallCartonNo());
							}
							smallNo++;
							smallCount++;
							smallEnchase.setEnchaseList(smallList);
							bigList.add(smallEnchase);
							if (smallCartonNum==bigCartonType) {
								bigEnchase.setBigCartonId(bigCartonId);
								if (flagNum == 0) {
									bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo));
								}else{
									bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
								}
								bigNo++;
								bigCount++;
								smallNo = 1;
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								bigEnchase = new CustomEnchase();
								bigList = new ArrayList<>();
								smallCartonNum = 0;
								rowSpan = 0;
							}
							smallEnchase = new CustomEnchase();
							smallList = new ArrayList<>();
						}
					}else {
						if (count<=smallCartonType&&count>1) {
							smallCartonNum++;
							count = 0;
							smallEnchase.setSmallCartonId(smallCartonId);
							if (flagNum == 0) {
								smallEnchase.setSmallCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo)+"."+f2.format(smallNo));
							}else{
								smallEnchase.setSmallCartonNo(orListSS.get(i).getSmallCartonNo());
							}
							smallNo = 1;
							smallCount++;
							smallEnchase.setEnchaseList(smallList);
							bigList.add(smallEnchase);
							if (smallCartonNum<=bigCartonType&&smallCartonNum>=1) {
								bigEnchase.setBigCartonId(bigCartonId);
								if (flagNum == 0) {
									bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo));
								}else{
									bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
								}
								bigNo = 1;
								bigCount++;
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								bigEnchase = new CustomEnchase();
								bigList = new ArrayList<>();
								smallCartonNum = 0;
								rowSpan = 0;
							/*}else if (smallCartonNum == 1) {
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								smallCartonNum = 0;
								bigEnchase = new CustomEnchase();
								bigList = new ArrayList<>();
								rowSpan = 0;
								bigNo = 1;*/
							}
							smallCartonNum = 0;
							rowSpan = 0;
						}else if(count == 1) {
							if (smallCartonNum<=bigCartonType&&smallCartonNum>=1) {
								bigEnchase.setBigCartonId(bigCartonId);
								if (flagNum == 0) {
									bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo));
								}else{
									bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
								}
								bigNo = 1;
								bigCount++;
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orListSS.get(i));
								bigEnchase = new CustomEnchase();
								bigList = new ArrayList<>();
								smallCartonNum = 0;
								rowSpan = 0;
							/*}else if (smallCartonNum == 1) {
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orListSS.get(i));
								bigEnchase = new CustomEnchase();
								bigList = new ArrayList<>();
								smallCartonNum = 0;
								rowSpan = 0;
								bigNo = 1;*/
							}else if (smallCartonNum == 0){
								allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orListSS.get(i));
								allList.get(allList.size()-1).setRowSpan(allList.get(allList.size()-1).getRowSpan()+1);
								smallCartonNum = 0;
								rowSpan = 0;
							}
							count = 0;
							bigEnchase = new CustomEnchase();
							bigList = new ArrayList<>();
							bigNo = 1;
							smallNo = 1;
						}
						smallEnchase = new CustomEnchase();
						smallList = new ArrayList<>();
					}
				}else if (i>0&&i<orListSS.size()-1&&!orListSS.get(i).getShippingDestintion().equals(orListSS.get(i+1).getShippingDestintion())&&orListSS.get(i).getShippingDestintion().equals(orListSS.get(i-1).getShippingDestintion())) {
					smallList.add(orListSS.get(i));
					count++;
					rowSpan++;
					if (orListSS.get(i).getShopName().equals(orListSS.get(i-1).getShopName())) {
						if (count<=smallCartonType&&count>1) {
							smallCartonNum++;
							count = 0;
							smallEnchase.setSmallCartonId(smallCartonId);
							if (flagNum == 0) {
								smallEnchase.setSmallCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo)+"."+f2.format(smallNo));
							}else{
								smallEnchase.setSmallCartonNo(orListSS.get(i).getSmallCartonNo());
							}
							smallNo = 1;
							smallCount++;
							smallEnchase.setEnchaseList(smallList);
							bigList.add(smallEnchase);
							if (smallCartonNum<=bigCartonType&&smallCartonNum>=1) {
								bigEnchase.setBigCartonId(bigCartonId);
								if (flagNum == 0) {
									bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo));
								}else{
									bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
								}
								bigNo = 1;
								bigCount++;
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								bigEnchase = new CustomEnchase();
								bigList = new ArrayList<>();
								smallCartonNum = 0;
								rowSpan = 0;
							/*}else if (smallCartonNum == 1) {
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								bigEnchase = new CustomEnchase();
								bigList = new ArrayList<>();
								smallCartonNum = 0;
								rowSpan = 0;
								bigNo = 1;*/
							}
							smallEnchase = new CustomEnchase();
							smallList = new ArrayList<>();
						}else if(count == 1) {
							if (smallCartonNum<=bigCartonType&&smallCartonNum>=1) {
								bigEnchase.setBigCartonId(bigCartonId);
								if (flagNum == 0) {
									bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo));
								}else{
									bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
								}
								bigNo = 1;
								bigCount++;
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orListSS.get(i));
								smallCartonNum = 0;
								rowSpan = 0;
							/*}else if (smallCartonNum == 1) {
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orListSS.get(i));
								smallCartonNum = 0;
								rowSpan = 0;
								bigNo = 1;*/
							}else if (smallCartonNum == 0){
								allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orListSS.get(i));
								allList.get(allList.size()-1).setRowSpan(allList.get(allList.size()-1).getRowSpan()+1);
								smallCartonNum = 0;
								rowSpan = 0;
							}
							count = 0;
							bigEnchase = new CustomEnchase();
							bigList = new ArrayList<>();
							smallEnchase = new CustomEnchase();
							smallList = new ArrayList<>();
							bigNo = 1;
							smallNo = 1;
						}
					}else {
						smallList.add(orListSS.get(i));
						smallEnchase.setSmallCartonId(smallCartonId);
						if (flagNum == 0) {
							smallEnchase.setSmallCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo)+"."+f2.format(smallNo));
						}else{
							smallEnchase.setSmallCartonNo(orListSS.get(i).getSmallCartonNo());
						}
						smallNo = 1;
						smallCount++;
						sSmallCount++;
						smallEnchase.setEnchaseList(smallList);
						bigList.add(smallEnchase);
						bigEnchase.setBigCartonId(bigCartonId);
						if (flagNum == 0) {
							bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "."+f1.format(bigNo));
						}else{
							bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
						}
						bigNo = 1;
						bigCount++;
						bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
						bigEnchase.setEnchases(bigList);
						bigEnchase.setRowSpan(1);
						allList.add(bigEnchase);
						smallEnchase = new CustomEnchase();
						smallList = new ArrayList<>();
						bigEnchase = new CustomEnchase();
						bigList = new ArrayList<>();
					}
				}else if (i>0&&i<orListSS.size()-1&&!orListSS.get(i).getShippingDestintion().equals(orListSS.get(i+1).getShippingDestintion())&&!orListSS.get(i).getShippingDestintion().equals(orListSS.get(i-1).getShippingDestintion())) {
					smallList.add(orListSS.get(i));
					smallEnchase.setSmallCartonId(smallCartonId);
					if (flagNum == 0) {
						smallEnchase.setSmallCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "."+f1.format(bigNo)+"."+f2.format(smallNo));
					}else{
						smallEnchase.setSmallCartonNo(orListSS.get(i).getSmallCartonNo());
					}
					smallNo = 1;
					smallCount++;
					sSmallCount++;
					smallEnchase.setEnchaseList(smallList);
					bigList.add(smallEnchase);
					bigEnchase.setBigCartonId(bigCartonId);
					if (flagNum == 0) {
						bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "."+f1.format(bigNo));
					}else{
						bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
					}
					bigNo = 1;
					bigCount++;
					bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
					bigEnchase.setEnchases(bigList);
					bigEnchase.setRowSpan(1);
					allList.add(bigEnchase);
					smallEnchase = new CustomEnchase();
					smallList = new ArrayList<>();
					bigEnchase = new CustomEnchase();
					bigList = new ArrayList<>();
					count = 0;
					smallCartonNum = 0;
					rowSpan = 0;
				}else if (i==orListSS.size()-1&&orListSS.get(i).getShippingDestintion().equals(orListSS.get(i-1).getShippingDestintion())) {
					smallList.add(orListSS.get(i));
					count++;
					rowSpan++;
					if (orListSS.get(i).getShopName().equals(orListSS.get(i-1).getShopName())) {
						if (count<=smallCartonType&&count>1) {
							smallCartonNum++;
							count = 0;
							smallEnchase.setSmallCartonId(smallCartonId);
							if (flagNum == 0) {
								smallEnchase.setSmallCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo)+"."+f2.format(smallNo));
							}else{
								smallEnchase.setSmallCartonNo(orListSS.get(i).getSmallCartonNo());
							}
							smallNo = 1;
							smallCount++;
							smallEnchase.setEnchaseList(smallList);
							bigList.add(smallEnchase);
							if (smallCartonNum<=bigCartonType&&smallCartonNum>=1) {
								bigEnchase.setBigCartonId(bigCartonId);
								if (flagNum == 0) {
									bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo));
								}else{
									bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
								}
								bigNo = 1;
								bigCount++;
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								bigEnchase = new CustomEnchase();
								bigList = new ArrayList<>();
								smallCartonNum = 0;
							/*}else if (smallCartonNum == 1) {
								sSmallCount++;
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								bigEnchase = new CustomEnchase();
								bigList = new ArrayList<>();
								smallCartonNum = 0;
								bigNo = 1;*/
							}
							smallEnchase = new CustomEnchase();
							smallList = new ArrayList<>();
						}else if(count == 1) {
							if (smallCartonNum<=bigCartonType&&smallCartonNum>=1) {
								bigEnchase.setBigCartonId(bigCartonId);
								if (flagNum == 0) {
									bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "." +f1.format(bigNo));
								}else{
									bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
								}
								bigNo = 1;
								bigCount++;
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orListSS.get(i));
								smallCartonNum = 0;
								rowSpan = 0;
							/*}else if (smallCartonNum == 1) {
								bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
								bigEnchase.setEnchases(bigList);
								bigEnchase.setRowSpan(rowSpan);
								allList.add(bigEnchase);
								allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orListSS.get(i));
								smallCartonNum = 0;
								rowSpan = 0;
								bigNo = 1;*/
							}else if (smallCartonNum == 0){
								allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orListSS.get(i));
								allList.get(allList.size()-1).setRowSpan(allList.get(allList.size()-1).getRowSpan()+1);
								smallCartonNum = 0;
								rowSpan = 0;
							}
							count = 0;
							bigEnchase = new CustomEnchase();
							bigList = new ArrayList<>();
							smallEnchase = new CustomEnchase();
							smallList = new ArrayList<>();
							bigNo = 1;
							smallNo = 1;
						}
					}else {
						smallList.add(orListSS.get(i));
						smallEnchase.setSmallCartonId(smallCartonId);
						if (flagNum == 0) {
							smallEnchase.setSmallCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase() +"."+f1.format(bigNo)+"."+f2.format(smallNo));
						}else{
							smallEnchase.setSmallCartonNo(orListSS.get(i).getSmallCartonNo());
						}
						smallNo = 1;
						smallCount++;
						smallEnchase.setEnchaseList(smallList);
						bigList.add(smallEnchase);
						bigEnchase.setBigCartonId(bigCartonId);
						if (flagNum == 0) {
							bigEnchase.setBigCartonNo(dateStr + "SS." + orListSS.get(i).getShopName().substring(orListSS.get(i).getShopName().length()-2, orListSS.get(i).getShopName().length()).toUpperCase()+ "."+f1.format(bigNo));
						}else{
							bigEnchase.setBigCartonNo(orListSS.get(i).getBigCartonNo());
						}
						bigNo = 1;
						bigCount++;
						bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
						bigEnchase.setEnchases(bigList);
						bigEnchase.setRowSpan(1);
						allList.add(bigEnchase);
						smallEnchase = new CustomEnchase();
						smallList = new ArrayList<>();
						bigEnchase = new CustomEnchase();
						bigList = new ArrayList<>();
					}
				}else if (i==orListSS.size()-1&&!orListSS.get(i).getShippingDestintion().equals(orListSS.get(i-1).getShippingDestintion())) {
					smallList.add(orListSS.get(i));
					smallEnchase.setSmallCartonId(smallCartonId);
					if (flagNum == 0) {
						smallEnchase.setSmallCartonNo(dateStr + getSimpleName(orListSS.get(i).getShippingDestintion()) +"."+f1.format(bigNo)+"."+f2.format(smallNo));
					}else{
						smallEnchase.setSmallCartonNo(orListSS.get(i).getSmallCartonNo());
					}
					smallNo = 1;
					smallCount++;
					sSmallCount++;
					smallEnchase.setEnchaseList(smallList);
					bigList.add(smallEnchase);
					bigEnchase.setShippingDestintion(orListSS.get(i).getShippingDestintion());
					bigEnchase.setEnchases(bigList);
					bigEnchase.setRowSpan(1);
					allList.add(bigEnchase);
					smallEnchase = new CustomEnchase();
					smallList = new ArrayList<>();
					bigEnchase = new CustomEnchase();
					bigList = new ArrayList<>();
				}
			}
		}
		rowSpan = 0;
		//除SS澳大利亚的其他目的地
		if (orList != null && orList.size() > 0) {
			for (int i = 0; i < orList.size(); i++) {
				if (orList.get(i).getShopName() == null || "".equals(orList.get(i).getShopName())) {
					throw new RuntimeException(orList.get(i).getOrderNO()+"订单装箱信息不全！Info_Incomplete"); 
				}
				if (i==0&&i<orList.size()-1&&orList.get(i).getShippingDestintion().equals(orList.get(i+1).getShippingDestintion())) {
					smallList.add(orList.get(i));
					count++;
					rowSpan++;
				}else if ((i==0&&orList.size()==1)||(i==0&&i<orList.size()-1&&!orList.get(i).getShippingDestintion().equals(orList.get(i+1).getShippingDestintion()))) {
					smallList.add(orList.get(i));
					smallEnchase.setSmallCartonId(smallCartonId);
					if (flagNum == 0) {
						smallEnchase.setSmallCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo)+"."+f2.format(smallNo));
					}else{
						smallEnchase.setSmallCartonNo(orList.get(i).getSmallCartonNo());
					}
					smallNo = 1;
					smallCount++;
					sSmallCount++;
					smallEnchase.setEnchaseList(smallList);
					bigList.add(smallEnchase);
					if (flagNum == 0) {
						bigEnchase.setBigCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo));
					}else{
						bigEnchase.setBigCartonNo(orList.get(i).getBigCartonNo());
					}
					bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
					bigEnchase.setEnchases(bigList);
					bigEnchase.setRowSpan(1);
					allList.add(bigEnchase);
					smallEnchase = new CustomEnchase();
					smallList = new ArrayList<>();
					bigEnchase = new CustomEnchase();
					bigList = new ArrayList<>();
				}else if (i>0&&i<orList.size()-1&&orList.get(i).getShippingDestintion().equals(orList.get(i+1).getShippingDestintion())){
					smallList.add(orList.get(i));
					count++;
					rowSpan++;
					if (count==smallCartonType) {
						smallCartonNum++;
						count = 0;
						smallEnchase.setSmallCartonId(smallCartonId);
						if (flagNum == 0) {
							smallEnchase.setSmallCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo)+"."+f2.format(smallNo));
						}else{
							smallEnchase.setSmallCartonNo(orList.get(i).getSmallCartonNo());
						}
						smallNo++;
						smallCount++;
						smallEnchase.setEnchaseList(smallList);
						bigList.add(smallEnchase);
						if (smallCartonNum==bigCartonType) {
							bigEnchase.setBigCartonId(bigCartonId);
							if (flagNum == 0) {
								bigEnchase.setBigCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo));
							}else{
								bigEnchase.setBigCartonNo(orList.get(i).getBigCartonNo());
							}
							bigNo++;
							bigCount++;
							smallNo = 1;
							bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
							bigEnchase.setEnchases(bigList);
							bigEnchase.setRowSpan(rowSpan);
							allList.add(bigEnchase);
							bigEnchase = new CustomEnchase();
							bigList = new ArrayList<>();
							smallCartonNum = 0;
							rowSpan = 0;
						}
						smallEnchase = new CustomEnchase();
						smallList = new ArrayList<>();
					}
					
				}else if (i>0&&i<orList.size()-1&&!orList.get(i).getShippingDestintion().equals(orList.get(i+1).getShippingDestintion())&&orList.get(i).getShippingDestintion().equals(orList.get(i-1).getShippingDestintion())) {
					smallList.add(orList.get(i));
					count++;
					rowSpan++;
					if (count<=smallCartonType&&count>1) {
						smallCartonNum++;
						count = 0;
						smallEnchase.setSmallCartonId(smallCartonId);
						if (flagNum == 0) {
							smallEnchase.setSmallCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo)+"."+f2.format(smallNo));
						}else{
							smallEnchase.setSmallCartonNo(orList.get(i).getSmallCartonNo());
						}
						smallNo = 1;
						smallCount++;
						smallEnchase.setEnchaseList(smallList);
						bigList.add(smallEnchase);
						if (smallCartonNum<=bigCartonType&&smallCartonNum>=1) {
							bigEnchase.setBigCartonId(bigCartonId);
							if (flagNum == 0) {
								bigEnchase.setBigCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo));
							}else{
								bigEnchase.setBigCartonNo(orList.get(i).getBigCartonNo());
							}
							bigNo = 1;
							bigCount++;
							bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
							bigEnchase.setEnchases(bigList);
							bigEnchase.setRowSpan(rowSpan);
							allList.add(bigEnchase);
							bigEnchase = new CustomEnchase();
							bigList = new ArrayList<>();
							smallCartonNum = 0;
							rowSpan = 0;
						/*}else if (smallCartonNum == 1) {
							sSmallCount++;
							bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
							bigEnchase.setEnchases(bigList);
							bigEnchase.setRowSpan(rowSpan);
							allList.add(bigEnchase);
							bigEnchase = new CustomEnchase();
							bigList = new ArrayList<>();
							smallCartonNum = 0;
							rowSpan = 0;
							bigNo = 1;*/
						}
						smallEnchase = new CustomEnchase();
						smallList = new ArrayList<>();
					}else if(count == 1) {
						if (smallCartonNum<=bigCartonType&&smallCartonNum>=1) {
							bigEnchase.setBigCartonId(bigCartonId);
							if (flagNum == 0) {
								bigEnchase.setBigCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo));
							}else{
								bigEnchase.setBigCartonNo(orList.get(i).getBigCartonNo());
							}
							bigNo = 1;
							bigCount++;
							bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
							bigEnchase.setEnchases(bigList);
							bigEnchase.setRowSpan(rowSpan);
							allList.add(bigEnchase);
							allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orList.get(i));
							smallCartonNum = 0;
							rowSpan = 0;
						/*}else if (smallCartonNum == 1) {
							bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
							bigEnchase.setEnchases(bigList);
							bigEnchase.setRowSpan(rowSpan);
							allList.add(bigEnchase);
							allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orList.get(i));
							smallCartonNum = 0;
							rowSpan = 0;
							bigNo = 1;*/
						}else if (smallCartonNum == 0){
							allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orList.get(i));
							allList.get(allList.size()-1).setRowSpan(allList.get(allList.size()-1).getRowSpan()+1);
							smallCartonNum = 0;
							rowSpan = 0;
						}
						count = 0;
						bigEnchase = new CustomEnchase();
						bigList = new ArrayList<>();
						smallEnchase = new CustomEnchase();
						smallList = new ArrayList<>();
						bigNo = 1;
						smallNo = 1;
					}
				}else if (i>0&&i<orList.size()-1&&!orList.get(i).getShippingDestintion().equals(orList.get(i+1).getShippingDestintion())&&!orList.get(i).getShippingDestintion().equals(orList.get(i-1).getShippingDestintion())) {
					smallList.add(orList.get(i));
					smallEnchase.setSmallCartonId(smallCartonId);
					if (flagNum == 0) {
						smallEnchase.setSmallCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo)+"."+f2.format(smallNo));
					}else{
						smallEnchase.setSmallCartonNo(orList.get(i).getSmallCartonNo());
					}
					smallNo = 1;
					smallCount++;
					sSmallCount++;
					smallEnchase.setEnchaseList(smallList);
					bigEnchase.setBigCartonId(bigCartonId);
					if (flagNum == 0) {
						bigEnchase.setBigCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo));
					}else{
						bigEnchase.setBigCartonNo(orList.get(i).getBigCartonNo());
					}
					bigNo = 1;
					bigCount++;
					bigList.add(smallEnchase);
					bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
					bigEnchase.setEnchases(bigList);
					bigEnchase.setRowSpan(1);
					allList.add(bigEnchase);
					smallEnchase = new CustomEnchase();
					smallList = new ArrayList<>();
					bigEnchase = new CustomEnchase();
					bigList = new ArrayList<>();
					count = 0;
					smallCartonNum = 0;
					rowSpan = 0;
				}else if (i==orList.size()-1&&orList.get(i).getShippingDestintion().equals(orList.get(i-1).getShippingDestintion())) {
					smallList.add(orList.get(i));
					count++;
					rowSpan++;
					if (count<=smallCartonType&&count>1) {
						smallCartonNum++;
						count = 0;
						smallEnchase.setSmallCartonId(smallCartonId);
						if (flagNum == 0) {
							smallEnchase.setSmallCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo)+"."+f2.format(smallNo));
						}else{
							smallEnchase.setSmallCartonNo(orList.get(i).getSmallCartonNo());
						}
						smallNo = 1;
						smallCount++;
						smallEnchase.setEnchaseList(smallList);
						bigList.add(smallEnchase);
						if (smallCartonNum<=bigCartonType&&smallCartonNum>=1) {
							bigEnchase.setBigCartonId(bigCartonId);
							if (flagNum == 0) {
								bigEnchase.setBigCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo));
							}else{
								bigEnchase.setBigCartonNo(orList.get(i).getBigCartonNo());
							}
							bigNo = 1;
							bigCount++;
							bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
							bigEnchase.setEnchases(bigList);
							bigEnchase.setRowSpan(rowSpan);
							allList.add(bigEnchase);
							bigEnchase = new CustomEnchase();
							bigList = new ArrayList<>();
							smallCartonNum = 0;
						/*}else if (smallCartonNum == 1) {
							sSmallCount++;
							bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
							bigEnchase.setEnchases(bigList);
							bigEnchase.setRowSpan(rowSpan);
							allList.add(bigEnchase);
							bigEnchase = new CustomEnchase();
							bigList = new ArrayList<>();
							smallCartonNum = 0;
							bigNo = 1;*/
						}
						smallEnchase = new CustomEnchase();
						smallList = new ArrayList<>();
					}else if(count == 1) {
						if (smallCartonNum<=bigCartonType&&smallCartonNum>=1) {
							bigEnchase.setBigCartonId(bigCartonId);
							if (flagNum == 0) {
								bigEnchase.setBigCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo));
							}else{
								bigEnchase.setBigCartonNo(orList.get(i).getBigCartonNo());
							}
							bigNo = 1;
							bigCount++;
							bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
							bigEnchase.setEnchases(bigList);
							bigEnchase.setRowSpan(rowSpan);
							allList.add(bigEnchase);
							allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orList.get(i));
							smallCartonNum = 0;
							rowSpan = 0;
						/*}else if (smallCartonNum == 1) {
							bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
							bigEnchase.setEnchases(bigList);
							bigEnchase.setRowSpan(rowSpan);
							allList.add(bigEnchase);
							allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orList.get(i));
							smallCartonNum = 0;
							rowSpan = 0;
							bigNo = 1;*/
						}else if (smallCartonNum == 0){
							allList.get(allList.size()-1).getEnchases().get(allList.get(allList.size()-1).getEnchases().size()-1).getEnchaseList().add(orList.get(i));
							allList.get(allList.size()-1).setRowSpan(allList.get(allList.size()-1).getRowSpan()+1);
							smallCartonNum = 0;
							rowSpan = 0;
						}
						count = 0;
						bigEnchase = new CustomEnchase();
						bigList = new ArrayList<>();
						smallEnchase = new CustomEnchase();
						smallList = new ArrayList<>();
						bigNo = 1;
						smallNo = 1;
					}
				}else if (i==orList.size()-1&&!orList.get(i).getShippingDestintion().equals(orList.get(i-1).getShippingDestintion())) {
					smallList.add(orList.get(i));
					smallEnchase.setSmallCartonId(smallCartonId);
					if (flagNum == 0) {
						smallEnchase.setSmallCartonNo(dateStr + getSimpleName(orList.get(i).getShippingDestintion()) +"."+f1.format(bigNo)+"."+f2.format(smallNo));
					}else{
						smallEnchase.setSmallCartonNo(orList.get(i).getSmallCartonNo());
					}
					smallNo = 1;
					smallCount++;
					sSmallCount++;
					smallEnchase.setEnchaseList(smallList);
					bigList.add(smallEnchase);
					bigEnchase.setShippingDestintion(orList.get(i).getShippingDestintion());
					bigEnchase.setEnchases(bigList);
					bigEnchase.setRowSpan(1);
					allList.add(bigEnchase);
					smallEnchase = new CustomEnchase();
					smallList = new ArrayList<>();
					bigEnchase = new CustomEnchase();
					bigList = new ArrayList<>();
				}
			}
		}
		orList.addAll(orListSS);
		enchase.setEnchases(allList);
		enchase.setContracts(contractList);
		enchase.setContractCount(orList.size());
		enchase.setBigCount(bigCount);
		enchase.setSmallCount(smallCount);
		enchase.setVolume(smallCartonVolume*sSmallCount+bigCartonVolume*bigCount);
		enchase.setNetWeight(smallCartonWeight*smallCount);
		enchase.setGrossWeight(smallCartonWeight*smallCount);
		return enchase;
	}
	
	/**
	 * 
	 * 
	 */

	public String getSimpleName(String Tyy) {
		//1.SS澳大利亚
		if ("1".equals(Tyy.replace(".", ",").split(",")[0])) {
			return "SS";
		//2.欧洲
		}else if ("2".equals(Tyy.replace(".", ",").split(",")[0])) {
			return "EU";
		//3.IT 俄罗斯
		}else if ("3".equals(Tyy.replace(".", ",").split(",")[0])) {
			return "IT";
		//4.NG 俄罗斯
		}else if ("4".equals(Tyy.replace(".", ",").split(",")[0])) {
			return "NG";
		//6.SU美国
		}else if ("6".equals(Tyy.replace(".", ",").split(",")[0])) {
			return "SU";
		//7.中国
		}else if ("7".equals(Tyy.replace(".", ",").split(",")[0])) {
			return "CN";
		//8.多伦多
		}else if ("8".equals(Tyy.replace(".", ",").split(",")[0])) {
			return "TO";
		//11.SU 北京
		}else if ("11".equals(Tyy.replace(".", ",").split(",")[0]) || "北京".equals(Tyy)) {
			return "BJ";
		//12.SU 香港
		}else if ("12".equals(Tyy.replace(".", ",").split(",")[0])) {
			return "HK";
		//13.SU 韩国
		}else if ("13".equals(Tyy.replace(".", ",").split(",")[0])) {
			return "KO";
		//15.SU 杭州
		}else if ("15".equals(Tyy.replace(".", ",").split(",")[0])) {
			return "HZ";
		//大连
		}else if ("大连".equals(Tyy)) {
			return "DL";
		//沈阳
		}else if ("沈阳".equals(Tyy)) {
			return "SY";
		//上海
		}else if ("上海".equals(Tyy)) {
			return "SH";
		//深圳
		}else if ("深圳".equals(Tyy)) {
			return "SZ";
		}else {
			return "";
		}
	}
	
	/**
	 * 
	 * 
	 */
	public List<CustomEnchase> makeExportList(CustomEnchase enchase,String yunStr){
		List<CustomEnchase> enchases = enchaseDao.findEnchaseList(enchase);
		String bigCarton = "";
		String smallCarton = "";
		if (enchases!=null&&enchases.size()>0) {
			for (int i = 0; i < enchases.size(); i++) {
				/*if (!bigCarton.equals(enchases.get(i).getBigCartonNo())) {
					if ("".equals(enchases.get(i).getBigCartonNo())||enchases.get(i).getBigCartonNo()==null) {
						enchases.get(i).setBigCartonNo(yunStr);
						bigCarton = "";
					}else {
						bigCarton = enchases.get(i).getBigCartonNo();
					}
				}else {
					enchases.get(i).setBigCartonNo("");
				}
				if (!smallCarton.equals(enchases.get(i).getSmallCartonNo())) {
					smallCarton = enchases.get(i).getSmallCartonNo();
				}else {
					enchases.get(i).setSmallCartonNo("");
				}*/
				if (!"".equals(enchases.get(i).getUrgentCD())&&enchases.get(i).getUrgentCD()!=null) {
					enchases.get(i).setUrgentCD(DictUtils.getDictName(enchases.get(i).getUrgentCD()));
				}
				if (enchases.get(i).getSleeveFlag() == 0) {
					enchases.get(i).setSleeveFlagStr("长袖");
				}else if (enchases.get(i).getSleeveFlag() == 1) {
					enchases.get(i).setSleeveFlagStr("短袖");
				}
			}
		}
		return enchases;
	}
	
   /**
    * @Title: findAuditList
    * @Description: 查询所有履历
    * @param @param auditInfo
    * @param @return
    * @return List<AuditInfo> 
    * @throws
    */
  public AuditInfo findAuditInfo(AuditInfo auditInfo) {
	   AuditInfo audit = new AuditInfo();
	   audit.setAuditList(auditInfoDao.findAllList(auditInfo));
	   audit.setAuditStatus(auditInfoDao.findAuditAcceptCount(auditInfo));
      // 执行查询
      return audit;
  }
  
  /**
   * @Title: getAuditResult
   * @Description: 审核结果
   * @param @param auditInfo
   * @param @return
   * @return 审核结果通过数
   * @throws
   */
 
 public long getAuditResult(AuditInfo auditInfo) {
	   
     return auditInfoDao.findAuditAcceptCount(auditInfo);
 }
 
	/**
	  * @Title: saveAudit
	  * @Description: 保存履历信息
	  * @param @param auditInfo
	  * @return void 
	  * @throws
	  */
	@Transactional(readOnly = false)
	public void saveAuditInfo(AuditInfo auditInfo) {
		
		 // 插入履历表
	     auditInfo.preInsert();
	     auditInfo.setAuditDate(new Date());
	     auditInfo.setAuditUserId(UserUtils.getUser().getId());
        // 新建版本号赋值1
	     auditInfo.setVersion(1L);
	     auditInfoDao.insert(auditInfo);
	     
	     // 修正箱单状态
	     auditInfo.preUpdate();
	     enchaseDao.updateCustomEnchaseStatus(auditInfo);
	}
	
}
